package com.qf.smsplatform.interfaces.config;

import com.qf.smsplatform.common.constants.RabbitMQConstants;
import com.rabbitmq.client.impl.AMQImpl;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;


/**
 * 创建队列，接口模块给策略模块的消息队列
 * @author zjw
 */
@Configuration
public class RabbitMQConfig {

    public static final String DELAYED_EXCHANGE = "delayed_exchange";
    public static final String DELAYED_QUEUE = "delayed_queue";
    public static final String DELAYED_TYPE = "x-delayed-message";

    /**
     * 构建的队列
     * @return
     */
    @Bean
    public Queue preSendQueue(){
        return QueueBuilder.durable(RabbitMQConstants.TOPIC_PRE_SEND).build();
    }


    @Bean
    public Exchange delayedExchange(){
        Map<String, Object> args = new HashMap<>();
        // 交换机类型是采用fanout的方式路由
        args.put("x-delayed-type","fanout");
        // 交换机是一个延迟交换机
        Exchange exchange = new CustomExchange(DELAYED_EXCHANGE,DELAYED_TYPE,true,false,args);
        return exchange;
    }

    @Bean
    public Queue delayedQueue(){
        return QueueBuilder.durable(DELAYED_QUEUE).build();
    }

    @Bean
    public Binding delayedBinding(Queue delayedQueue,Exchange delayedExchange){
        return BindingBuilder.bind(delayedQueue).to(delayedExchange).with("").noargs();
    }

}
